import traer.physics.*;

class Shot {
  traer.physics.ParticleSystem physics;
  traer.physics.Particle[] bullets;
  int maxBullets = 10;
  int curIndex = 0;

  void init() {
    physics = new traer.physics.ParticleSystem(5.0,0.05);
    bullets = new traer.physics.Particle[maxBullets];
    for(int i=0; i < maxBullets; i++) {
      bullets[i] = physics.makeParticle(2,0,0,0);
      bullets[i].makeFixed();
    }
    physics.setGravity(0,0,0);
    //physics.setDrag(0);
  }

  void spawnshot(traer.physics.Particle i) {
    bullets[curIndex].makeFree();
    float x,y,vx,vy;
    x = i.position().x();
    y = i.position().y();
    bullets[curIndex].moveTo(x,y,0.0);
    vx = (bullets[curIndex].position().x() - mouseX) * invWidth;
    vy = (bullets[curIndex].position().y() - mouseY) * invHeight;
    bullets[curIndex].addVelocity(30*vx*-1,30*vy*-1,0);
    curIndex++;
    if(curIndex >= 10) curIndex = 0;
  }

  void updatePhysics() {
    float _x,_y,_vx,_vy;
    float _px[] = new float[maxBullets];
    float _py[] = new float[maxBullets];
    PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
    GL gl = pgl.beginGL();
    gl.glEnable(GL.GL_BLEND);
    fadeToColor(gl,0,0,0,0.5);
    gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
    gl.glEnable(GL.GL_LINE_SMOOTH);
    gl.glLineWidth(1);
    gl.glBegin(GL.GL_QUADS);

    for(int i = 0; i < maxBullets; i++) {
      _px[i] = bullets[i].position().x();
      _py[i] = bullets[i].position().y();
      if(abs(bullets[i].velocity().x()) < 0.2 && abs(bullets[i].velocity().y()) < 0.2)
        bullets[i].moveTo(-2,-2,0);
    }
    physics.advanceTime(1.0);
    for(int i = 0; i < maxBullets; i++) {
      _x = bullets[i].position().x();
      _y = bullets[i].position().y();
      _vx = (_x-_px[i])*invWidth;
      _vy = (_y-_py[i])*invHeight;
      if(!(abs(bullets[i].velocity().x()) < 0.2 && abs(bullets[i].velocity().y()) < 0.2)) {
        addForce(_x*invWidth,_y*invHeight*1.25,_vx,_vy);
        addForce(_x*invWidth,_y*invHeight*1.25,_vx,_vy);
        addForce(_x*invWidth,_y*invHeight*1.25,_vx,_vy);
        addForce(_x*invWidth,_y*invHeight*1.25,_vx,_vy);
      }
    }

    drawOldSchool(gl);
    gl.glEnd();
    gl.glDisable(GL.GL_BLEND);
    pgl.endGL();
  }

  void drawOldSchool(GL gl) {
    gl.glColor3f(1.0f,1.0f,1.0f);
    for(int i=0; i < maxBullets; i++) {
      gl.glVertex2f(bullets[i].position().x()-2,bullets[i].position().y()-2);
      gl.glVertex2f(bullets[i].position().x()+2,bullets[i].position().y()-2);
      gl.glVertex2f(bullets[i].position().x()+2,bullets[i].position().y()+2);
      gl.glVertex2f(bullets[i].position().x()-2,bullets[i].position().y()+2);
    }
  }
}





